//
//  LHSingerListViewController.m
//  Enjoy_Music
//
//  Created by Bruce_Lin on 15/6/11.
//  Copyright (c) 2015年 Bruce. All rights reserved.
//

#import "LHSingerListViewController.h"
#import "LHSingerListTableViewCell.h"
#import "LHSingerListModel.h"
#import <UIImageView+WebCache.h>
#import "MyNavgationView.h"
#import "LHSingerDetailViewController.h"

#define KSingerListTableViewCell @"SingerListReuse"
@interface LHSingerListViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic ,strong)UITableView *listTableView;
@property (nonatomic ,strong)NSMutableArray *mainArr;
@property (nonatomic ,copy)NSString *path;
@property (nonatomic ,strong)MyNavgationView *navi;
@end

@implementation LHSingerListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.navi];
    [self.view addSubview:self.listTableView];
    
    [self getdata];
}

- (void)getdata
{
    [LHHttpHandler get:self.path body:nil result:LHHttp success:^(id result) {
        
        NSLog(@"%@",result);
        
        for (NSDictionary *dic in [result objectForKey:@"data"]) {
            
            LHSingerListModel *model = [[LHSingerListModel alloc]initWithDic:dic];
            
            [self.mainArr addObject:model];
        }
        
        [_listTableView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.mainArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LHSingerListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:KSingerListTableViewCell];
    
    [cell.headPic sd_setImageWithURL:[NSURL URLWithString:[_mainArr[indexPath.row] pic_url]] placeholderImage:nil];
    
    cell.backgroundColor = [UIColor clearColor];
    
    cell.nameLabel.text = [_mainArr[indexPath.row] singer_name];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return LHHeight / 7;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    LHSingerDetailViewController *vc = [[LHSingerDetailViewController alloc]init];
    vc.singerID = [_mainArr[indexPath.row] singer_id];
    [self.navigationController pushViewController:vc animated:YES];
}

/**
 *  listTableView懒加载
 *
 *  @return listTableView
 */
- (UITableView *)listTableView
{
    if (!_listTableView) {
        
        self.listTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, LHWidth, LHHeight - 64) style:UITableViewStylePlain];
        _listTableView.dataSource = self;
        _listTableView.delegate = self;
        UIImageView *view = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"stars"]];
        [_listTableView setBackgroundView:view];
        [_listTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        [_listTableView registerClass:[LHSingerListTableViewCell class] forCellReuseIdentifier:KSingerListTableViewCell];
    }
    return  _listTableView;
}

- (MyNavgationView *)navi
{
    if (!_navi) {
        self.navi = [[MyNavgationView alloc]initWithFrame:CGRectMake(0, 0, LHWidth, 64)];
        [_navi creatTitle:@"歌手列表"];
        [_navi creatLeftButtonWithImage:@"btn_nav_back" target:self action:@selector(back)];
    }
    return _navi;
}

/**
 *  path懒加载
 *
 *  @return path
 */
- (NSString *)path
{
    if (!_path) {
        self.path = [NSString stringWithFormat:@"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=%@&size=1000&page=1&app=ttpod&v=v7.9.1.2015050518&uid=&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=8.2&active=1&net=2&openudid=7a702b310d3c0910aca4228a673a841751162b63&idfa=E1922CE5-ECA3-494B-9B6A-B637D35AFDC2&utdid=VXEJsNE34hwDAGokQcEmljVB&alf=201200&bundle_id=com.ttpod.music",self.listId];
    }
    return _path;
}

/**
 *  mainArr懒加载
 *
 *  @return mainArr
 */
- (NSMutableArray *)mainArr
{
    if (!_mainArr) {
        self.mainArr = [NSMutableArray array];
    }
    return _mainArr;
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
